﻿Public Class AjoutEvenement
    Implements UserControlInterface

    Dim oLocalContext As DBMainContext
    Dim oLocalPlanEven As PlanificationEvenement
    Dim isAdding As Boolean = False
    Dim isModif As Boolean = False
    Dim isReading As Boolean = False
    Dim isModified As Boolean = True


    Private oEvenement As PlanificationEvenement
    Public ReadOnly Property Evenement() As PlanificationEvenement
        Get
            oEvenement = oLocalPlanEven
            With oEvenement
                .Nom = txtTitre.Text
                .Description = txtDesc.Text
                .APartirDe = dtpDebut.Value
                If rbLE.Checked Then
                    .Le = dtpLe.Value
                Else
                    .Le = Nothing
                End If
                If rbTOUSLESSem.Checked Then
                    .ToutLes = ""
                    If cbLundi.Checked Then
                        .ToutLes += "1"
                    Else
                        .ToutLes += "0"
                    End If
                    If cbMardi.Checked Then
                        .ToutLes += "1"
                    Else
                        .ToutLes += "0"
                    End If
                    If cbMercredi.Checked Then
                        .ToutLes += "1"
                    Else
                        .ToutLes += "0"
                    End If
                    If cbJeudi.Checked Then
                        .ToutLes += "1"
                    Else
                        .ToutLes += "0"
                    End If
                    If cbVendredi.Checked Then
                        .ToutLes += "1"
                    Else
                        .ToutLes += "0"
                    End If
                    If cbSamedi.Checked Then
                        .ToutLes += "1"
                    Else
                        .ToutLes += "0"
                    End If
                    If cbDimanche.Checked Then
                        .ToutLes += "1"
                    Else
                        .ToutLes += "0"
                    End If
                    .NbrSemaine = nudRepetition.Value
                Else
                    .ToutLes = Nothing
                End If
                If rbTOUSLESJOURS.Checked Then
                    .TousLesJours = True
                Else
                    .TousLesJours = Nothing
                End If

                If rbTOUSLES.Checked Then
                    .TousLes = nudChqMois.Value
                End If
                .Type = cbType.SelectedIndex + 1

                .Heure_Debut = dtpDe.Value
                .Heure_Fin = dtpA.Value


            End With
            Return oEvenement
        End Get

    End Property


    Public Sub New(ByRef oActivite As PlanificationEvenement)

        Me.New(oActivite.Type)
        Dim CheckedFromDBEmp As New List(Of Employe)
        Dim CheckedFromDBBen As New List(Of Beneficiaire)
        oLocalPlanEven = oActivite
        isModif = True
        isAdding = False
        dtpDebut.MinDate = oActivite.APartirDe
        dtpDebut.Value = oActivite.APartirDe
        With oLocalPlanEven
            txtTitre.Text = .Nom
            txtDesc.Text = .Description

            If Not IsNothing(.TousLes) Then
                rbTOUSLES.Checked = True
                nudChqMois.Value = .TousLes

            End If

            If Not IsNothing(.TousLesJours) Then
                rbTOUSLESJOURS.Checked = True
            End If

            If Not IsNothing(.Le) Then
                rbLE.Checked = True
                dtpLe.Value = .Le
            End If

            If Not IsNothing(.ToutLes) Then
                rbTOUSLESSem.Checked = True
                cbLundi.Checked = CBool(.ToutLes(0).ToString)
                cbMardi.Checked = CBool(.ToutLes(1).ToString)
                cbMercredi.Checked = CBool(.ToutLes(2).ToString)
                cbJeudi.Checked = CBool(.ToutLes(3).ToString)
                cbVendredi.Checked = CBool(.ToutLes(4).ToString)
                cbSamedi.Checked = CBool(.ToutLes(5).ToString)
                cbDimanche.Checked = CBool(.ToutLes(6).ToString)
                nudRepetition.Value = .NbrSemaine
            End If
            If .Heure_Debut Is Nothing Then
                dtpDe.Value = Date.Now
            Else
                dtpDe.Value = .Heure_Debut
            End If

            If .Heure_Fin Is Nothing Then
                dtpA.Value = Date.Now
            Else
                dtpA.Value = .Heure_Fin
            End If

        End With

        CheckedFromDBEmp = (From u In oLocalContext.Evenement_Employe Where u.ID_Evenement = oLocalPlanEven.ID Select u.Employe).ToList
        CheckedFromDBBen = (From u In oLocalContext.Evenement_Beneficiaire Where u.ID_Evenement = oLocalPlanEven.ID Select u.Beneficiaire).ToList
        Dim i As Integer = 0
        Dim oList As List(Of Object)
        oList = (From u In clbBeneficiaire.Items Select u).ToList
        For Each ben In oList

            If (CheckedFromDBBen.Contains(CType(ben, Beneficiaire))) Then
                clbBeneficiaire.SetItemChecked(i, True)
            End If
            i += 1

        Next
        i = 0
        oList = (From u In clbEmploye.Items Select u).ToList
        For Each ben In oList

            If (CheckedFromDBEmp.Contains(CType(ben, Employe))) Then
                clbEmploye.SetItemChecked(i, True)
            End If
            i += 1

        Next

        For Each Control As Control In Me.Controls
            AddHandler Control.TextChanged, AddressOf CHANGED
        Next

    End Sub

    Private Sub CHANGED()
        isModified = True
    End Sub

    Public Sub New(ByVal oType As PlanificationEvenement.TypeEvenement)

        ' Cet appel est requis par le concepteur.
        InitializeComponent()
        isAdding = True
        ' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
        oLocalContext = New DBMainContext

        clbEmploye.DataSource = (From u In oLocalContext.Employe Where u.Actif = True Order By u.Nom Select u)
        clbEmploye.DisplayMember = "NomComplet"
        clbEmploye.ValueMember = "ID"

        clbBeneficiaire.DataSource = (From u In oLocalContext.Beneficiaire Where u.Actif = True Order By u.Nom Select u)
        clbBeneficiaire.DisplayMember = "NomComplet"
        clbBeneficiaire.ValueMember = "ID"



        dtpDebut.MinDate = Date.Now

        oLocalPlanEven = New PlanificationEvenement

        cbType.SelectedIndex = oType - 1

    End Sub


    Private Sub dtpDebut_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtpDebut.ValueChanged
        dtpLe.MinDate = dtpDebut.Value
    End Sub

    Public Function OkToClose() As Boolean Implements UserControlInterface.OkToClose
        Return Valider()
    End Function

    Function Valider() As Boolean
        Dim Valide As Boolean = True

        If cbType.SelectedIndex = -1 Then
            Outils.ErrorProviderFunctions.AddError(Me, cbType, My.Resources.MessageErreur.DoitContenirUneValeur)
            Valide = False
        Else
            Outils.ErrorProviderFunctions.RemoveError(Me, cbType)
        End If

        If txtTitre.Text = "" Then
            Outils.ErrorProviderFunctions.AddError(Me, txtTitre, My.Resources.MessageErreur.DoitContenirUneValeur)
            Valide = False
        Else
            Outils.ErrorProviderFunctions.RemoveError(Me, txtTitre)
        End If


        If txtDesc.Text = "" Then
            Outils.ErrorProviderFunctions.AddError(Me, txtDesc, My.Resources.MessageErreur.DoitContenirUneValeur)
            Valide = False
        Else
            Outils.ErrorProviderFunctions.RemoveError(Me, txtDesc)
        End If

        If rbTOUSLESSem.Checked Then
            If Not (cbLundi.Checked Or cbMardi.Checked Or cbMercredi.Checked Or cbJeudi.Checked Or cbVendredi.Checked Or cbSamedi.Checked Or cbDimanche.Checked) Then
                Outils.ErrorProviderFunctions.AddError(Me, GroupBoxJourSem, My.Resources.MessageErreur.DoitChoisirUneJournee)
                Valide = False
            Else
                Outils.ErrorProviderFunctions.RemoveError(Me, GroupBoxJourSem)
            End If
        End If
        SaveEmploye()
        SaveBene()

        Return Valide


    End Function


    Private Sub btnSauvegarder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSauvegarder.Click
        Dim oValide As Boolean = Valider()

        If isModif And oValide Then
            Dim oBDItem As PlanificationEvenement = (From u In oLocalContext.PlanificationEvenement Where u.ID = oLocalPlanEven.ID Select u).FirstOrDefault
            Dim oLocalEven As PlanificationEvenement = Me.Evenement

            With oBDItem
                .APartirDe = oLocalEven.APartirDe
                .Description = oLocalEven.Description
                .Heure_Debut = oLocalEven.Heure_Debut
                .Heure_Fin = oLocalEven.Heure_Fin
                .Le = oLocalEven.Le
                .NbrSemaine = oLocalEven.NbrSemaine
                .Nom = oLocalEven.Nom
                .TousLes = oLocalEven.TousLes
                .TousLesJours = oLocalEven.TousLesJours
                .ToutLes = oLocalEven.ToutLes
                .Type = oLocalEven.Type

            End With

            oLocalContext.CustomSubmitChanges()
            Exit Sub
        End If

        If isAdding And oValide Then
            oLocalContext.PlanificationEvenement.InsertOnSubmit(Me.Evenement)
            oLocalContext.CustomSubmitChanges()
        End If


    End Sub

    Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtpDe.ValueChanged

    End Sub

    ''' <summary>
    ''' MIMIMIMIMIMI
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub SaveEmploye()
        Dim oCheckedEmployeFromDB As List(Of Evenement_Employe)
        oCheckedEmployeFromDB = (From u In oLocalContext.Evenement_Employe Where u.ID_Evenement = Me.Evenement.ID Select u).ToList


        For Each Ele As Employe In clbEmploye.CheckedItems
            If (From u In oCheckedEmployeFromDB Select u.Employe.ID).ToList.Contains(Ele.ID) Then
                'MsgBox("Present dans la BD et Dans la liste")
            Else
                Dim oNewEvenEmp As New Evenement_Employe
                oNewEvenEmp.Employe = CType(Ele, Employe)
                oNewEvenEmp.PlanificationEvenement = (From u In oLocalContext.PlanificationEvenement Where u.ID = oLocalPlanEven.ID Select u).FirstOrDefault
                oLocalContext.Evenement_Employe.InsertOnSubmit(oNewEvenEmp)
            End If
        Next

        For Each Ele As Evenement_Employe In oCheckedEmployeFromDB
            If Not (From u As Employe In clbEmploye.CheckedItems Select u.ID).ToList.Contains(Ele.Employe.ID) Then
                oLocalContext.Evenement_Employe.DeleteOnSubmit(Ele)
            End If
        Next
        oLocalContext.SubmitChanges()


    End Sub

    Private Sub SaveBene()
        Dim oCheckedBeneFromDB As List(Of Evenement_Beneficiaire)
        oCheckedBeneFromDB = (From u In oLocalContext.Evenement_Beneficiaire Where u.ID_Evenement = Me.Evenement.ID Select u).ToList


        For Each Ele As Beneficiaire In clbBeneficiaire.CheckedItems
            If (From u In oCheckedBeneFromDB Select u.Beneficiaire.ID).ToList.Contains(Ele.ID) Then
                'MsgBox("Present dans la BD et Dans la liste")
            Else
                Dim oNewEvenEmp As New Evenement_Beneficiaire
                oNewEvenEmp.Beneficiaire = CType(Ele, Beneficiaire)
                oNewEvenEmp.PlanificationEvenement = (From u In oLocalContext.PlanificationEvenement Where u.ID = oLocalPlanEven.ID Select u).FirstOrDefault
                oLocalContext.Evenement_Beneficiaire.InsertOnSubmit(oNewEvenEmp)
            End If
        Next

        For Each Ele As Evenement_Beneficiaire In oCheckedBeneFromDB
            If Not (From u As Employe In clbEmploye.CheckedItems Select u.ID).ToList.Contains(Ele.Beneficiaire.ID) Then
                oLocalContext.Evenement_Beneficiaire.DeleteOnSubmit(Ele)
            End If
        Next
        oLocalContext.SubmitChanges()
    End Sub





End Class
